package com.jiangnan.utils;

import com.jiangnan.constants.Constants;
import com.jiangnan.constants.MainButton;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 *
 *  swing组件工具类
 *
 * @author chenliang
 * @email wschenliang@aliyun.com
 */
public class SwingUtil {

    public static JTable getJTable(String[] columnName) {
        DefaultTableModel model = new DefaultTableModel(null, columnName);
        JTable jTable = new JTable(model){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;//不允许被编辑
            }

            @Override
            public boolean getScrollableTracksViewportWidth() {
                return getPreferredSize().width < getParent().getWidth();
            }
        };
        jTable.setRowHeight(25);//每条数据的单行高度
        jTable.setFont(Constants.GENERAL_FONT);
        jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);//设置表格，每次点击选择一个连续得范围
        notShow(jTable, 7,8);
        return jTable;
    }

    public static JComboBox<String> getComboBox(String[] selectItem) {
        JComboBox<String> box = new JComboBox<>(selectItem);
        box.setPreferredSize(new Dimension(150,25));
        return box;
    }

    public static JMenuItem getMenuItem(String text, ActionListener l) {
        JMenuItem item = new JMenuItem(text);
        item.setFont(Constants.GENERAL_FONT);
        item.addActionListener(l);
        return item;
    }

    public static JCheckBox getCheckBox(String text) {
        JCheckBox jCheckBox = new JCheckBox(text);
        jCheckBox.setFont(Constants.GENERAL_FONT);
        return jCheckBox;
    }

    public static JButton createButton(MainButton mainButton) {
        JButton button = new JButton();// 创建带有图标和文字的按钮
        button.setPreferredSize(new Dimension(180,180));
        Font bFont = new Font("宋体", Font.PLAIN, 20);
        button.setFont(bFont);
        button.setText(mainButton.getText());
        Icon icon = mainButton.getIcon();
        button.setIcon(icon);
        button.setVerticalTextPosition(SwingConstants.BOTTOM); // 设置文字在图标下方显示
        button.setHorizontalTextPosition(SwingConstants.CENTER); // 设置文字水平居中

        button.setBackground(Color.BLUE);
        button.setOpaque(true); // 设置为不透明以显示背景色
        button.setContentAreaFilled(true); // 填充内容区域背景色
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(button, "点击了 " + mainButton.getText());
            }
        });
        return button;
    }

    public static JTextArea getBottomArea(String text, boolean isEditable) {
        JTextArea jTextArea = new JTextArea(text) {
            @Override
            public boolean isEditable() {
                return isEditable;
            }
        };
        jTextArea.setFont(Constants.GENERAL_FONT);
        jTextArea.setCursor(new Cursor(Cursor.TEXT_CURSOR));//设置鼠标变为文本鼠标
        jTextArea.setBackground(Color.LIGHT_GRAY);
        return jTextArea;
    }

    //设置组件是否启用
    public static void EnableComponent(boolean isEnable, Component... components) {
        if (components == null) {
            return;
        }
        for (Component component : components) {
            component.setEnabled(isEnable);
        }
    }

    /**
     * 设置表格的某一行的背景色
     * @param table
     */
    public static void setOneRowBackgroundColor(JTable table, int rowIndex,
                                                Color color) {
        try {
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {

                public Component getTableCellRendererComponent(JTable table,
                                                               Object value, boolean isSelected, boolean hasFocus,
                                                               int row, int column) {
                    if (row == rowIndex) {
                        setBackground(color);
                        setForeground(Color.WHITE);
                    }
                    else if(row > rowIndex){
                        setBackground(Color.WHITE);
                        setForeground(Color.BLACK);
                    }else{
                        setBackground(Color.WHITE);
                        setForeground(Color.BLACK);
                    }

                    return super.getTableCellRendererComponent(table, value,
                            isSelected, hasFocus, row, column);
                }
            };
            int columnCount = table.getColumnCount();
            for (int i = 0; i < columnCount; i++) {
                table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //表格中那些数据不展示
    public static void notShow(JTable jTable, int startColumn, int endColumn) {
        TableColumnModel columnModel = jTable.getColumnModel();
        int columnCount = columnModel.getColumnCount();
        if (startColumn > columnCount) {
            LogUtils.log("不展示的数据超了");
            return;
        }
        if (endColumn >= columnCount) {
            endColumn = columnCount - 1;
        }
        for (int i = startColumn; i <= endColumn; i ++) {
            TableColumn column = columnModel.getColumn(i);
            column.setMaxWidth(0);
            column.setMinWidth(0);
            column.setPreferredWidth(0);
        }
    }

    public static JLabel getJLabel(String text) {
        JLabel jLabel = new JLabel(text);
        jLabel.setFont(new Font("宋体", Font.BOLD, 15));
        return jLabel;
    }

    public static JLabel getTitleJLabel(String text) {
        JLabel jLabel = new JLabel(text);
        jLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        return jLabel;
    }

    public static JSeparator getJSeparator(){
        JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);
        separator.setForeground(Color.BLACK); // 设置分隔线颜色为黑色
        Dimension dim = separator.getPreferredSize();
        dim.height = 3; // 设置分隔线高度
        separator.setPreferredSize(dim);
        return separator;
    }

}
